package com.yuhanginfo.modules.web.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.modules.web.util.WebUserUtils;
import com.yuhanginfo.modules.web.entity.WebCar;
import com.yuhanginfo.modules.web.entity.WebGoods;
import com.yuhanginfo.modules.web.entity.WebYongUser;
import com.yuhanginfo.modules.web.service.GoodsListService;
import com.yuhanginfo.modules.web.service.WebClassService;
import com.yuhanginfo.modules.web.service.WebYongUserService;
/**
 * 配件商城
 *
 */
@Controller
@RequestMapping(value = "${frontPath}/goodsList")
public class GoodsListController extends BaseController{
	@Autowired
	private GoodsListService goodsListService;
	@Autowired
	private WebClassService webClassService;
	@Autowired
	private WebYongUserService userService;
	
	/**
	 * 分页用
	 * @author lvls
	 */
	@RequestMapping(value = {"list", ""})
	public String list(Map<String,Object> params, HttpServletRequest request, HttpServletResponse response, Model model) {
		//判断用户是否登录
		WebYongUser yongUser=WebUserUtils.getYongUser();
		WebGoods WebGoods = new WebGoods();
		if(yongUser.getId()!=null&&yongUser.getId()!=""&&"2".equals(yongUser.getUserType())) {
			yongUser.setStoreList(userService.getStoreByUserId(yongUser.getId()));
			params.put("ids",yongUser.getStoreCodeList());
		}
		model.addAttribute("user",yongUser);
		//没登陆
		Page<WebGoods> page = goodsListService.getGoodsListByStoreCode(new Page<WebGoods>(request, response), WebGoods,params);
		List<WebGoods> goodsList = page.getList();
		model.addAttribute("params", params);
		//查询所有品牌
		List<WebCar> carBarandList=goodsListService.getAllCarBarand();
		model.addAttribute("carBarandList", carBarandList);
		if(StringUtils.isNotEmpty((String)params.get("carBarand"))) {
			model.addAttribute("carCodeList", goodsListService.getCarCodeByBarand((String)params.get("carBarand")));
		}
		if(StringUtils.isNotEmpty((String)params.get("carCode"))) {
			model.addAttribute("carYearList", goodsListService.getCarYearByCode((String)params.get("carCode")));
		}
		//PC端
		model.addAttribute("goodsList", goodsList);
		model.addAttribute("page", page);
		//手机端
		params.put("limitPage", 0);
		model.addAttribute("phoneGoodsList", goodsListService.getGoodsListByStoreCode(params));
		
		model.addAttribute("allClassesList",webClassService.getAllClasses());
		return "modules/web/partGoodsList";
	}
	
	@ResponseBody
	@RequestMapping(value = "getCarCodeByBarand")
	public List<String> getCarCodeByBarand(String barandCode) {
		List<String> list=new ArrayList<String>();
		List<String> result=goodsListService.getCarCodeByBarand(barandCode);
		for(String code:result) {
			if(code.contains("'")) {
				code=code.replace("'", "\'");
			}
			list.add(code);
		}
		return list;
	}
	
	@ResponseBody
	@RequestMapping(value = "getCarYearByCode")
	public List<String> getCarYearByCode(String code){
		return goodsListService.getCarYearByCode(code);
	}
	
	@RequestMapping(value = "getListByParam")
	public String getListByParam(Integer p,String venderType,String storeType,String isgoodsNum,String carBarand,String carBarandName,String carCode,String carYear,String searchContent, HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String,Object> params = new HashMap<String,Object>();
		if(StringUtils.isNotEmpty(venderType)) {
			params.put("venderType",venderType);
		}
		if(StringUtils.isNotEmpty(storeType)) {
			params.put("storeType",storeType);
		}
		if(StringUtils.isNotEmpty(isgoodsNum)) {
			params.put("isgoodsNum",isgoodsNum);
		}
		if(StringUtils.isNotEmpty(carBarandName)) {
			params.put("carBarandName",carBarandName);
		}
		if(StringUtils.isNotEmpty(carBarand)) {
			params.put("carBarand",carBarand);
		}
		if(StringUtils.isNotEmpty(carCode)) {
			params.put("carCode",carCode);
		}
		if(StringUtils.isNotEmpty(carYear)) {
			params.put("carYear",carYear);
		}
		if(StringUtils.isNotEmpty(searchContent)) {
			params.put("searchContent", searchContent);
		}
		if(p!=null&&p!=0) {
			int a=4*(p-1);
			params.put("a",a);
		}
		return list(params, request, response, model);
	}
	
	@RequestMapping(value="/selectPhoneList")
	public String selectPhoneList(Integer p,String venderType,String storeType,String isgoodsNum,String carBarand,String carBarandName,String carCode,String carYear, HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String,Object> params = new HashMap<String,Object>();
		if(StringUtils.isNotEmpty(venderType)) {
			params.put("venderType",venderType);
		}
		if(StringUtils.isNotEmpty(storeType)) {
			params.put("storeType",storeType);
		}
		if(StringUtils.isNotEmpty(isgoodsNum)) {
			params.put("isgoodsNum",isgoodsNum);
		}
		if(StringUtils.isNotEmpty(carBarand)) {
			params.put("carBarand",carBarand);
		}
		if(StringUtils.isNotEmpty(carBarandName)) {
			params.put("carBarandName",carBarandName);
		}
		if(StringUtils.isNotEmpty(carCode)) {
			params.put("carCode",carCode);
		}
		if(StringUtils.isNotEmpty(carYear)) {
			params.put("carYear",carYear);
		}
		if(p!=null&&p!=0) {
			int a=4*(p-1);
			params.put("limitPage",a);
		}
		//判断用户是否登录
		WebYongUser yongUser=WebUserUtils.getYongUser();
		if(yongUser.getId()!=null&&yongUser.getId()!="") {
			yongUser.setStoreList(userService.getStoreByUserId(yongUser.getId()));
			params.put("ids",yongUser.getStoreCodeList());
		}
		//没登陆
		List<WebGoods> goodsList=goodsListService.getGoodsListByStoreCode(params);
		model.addAttribute("user",yongUser);
		model.addAttribute("phoneGoodsList", goodsList);
		return "modules/web/goodsListPhone";
	}
}
